﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SFS.SharePoint.DynamicForms
{
    public class GenericHelper
    {
        public const string CONTROL_IDENTIFIER = "dynamic_ctl_";

        public static string CombineUrl(string baseUrl, string relativeUrl)
        {
            if (baseUrl.Length == 0)
                return relativeUrl;
            if (relativeUrl.Length == 0)
                return baseUrl;
            return string.Format("{0}/{1}", baseUrl.TrimEnd(new char[] { '/', '\\' }), relativeUrl.TrimStart(new char[] { '/', '\\' }));
        }

        public static string CombineUrl(string relativeUrl)
        {
            return CombineUrl(SPContext.Current.Web.ServerRelativeUrl, relativeUrl);
        }

        public static string ParseUrl(string RelativeUrl)
        {
            string ParsedUrl = string.Empty;
            if (RelativeUrl.StartsWith("~~~"))
                ParsedUrl = CombineUrl(SPContext.Current.Site.WebApplication.Sites[0].Url, RelativeUrl.TrimStart('~'));
            else if (RelativeUrl.StartsWith("~~"))
                ParsedUrl = CombineUrl(SPContext.Current.Site.Url, RelativeUrl.TrimStart('~'));
            else if (RelativeUrl.StartsWith("~"))
                ParsedUrl = CombineUrl(SPContext.Current.Web.Url, RelativeUrl.TrimStart('~'));
            else
                ParsedUrl = RelativeUrl;
            return ParsedUrl;

        }

        public static string ClosePopUp(string message)
        {
            var scriptBuilder = new StringBuilder();
            scriptBuilder.Append("<script type='text/javascript'>" + "SP.UI.ModalDialog.commonModalDialogClose(1,'").Append(message).Append("'); </script>");
            return scriptBuilder.ToString();
        }

        public enum InfoType
        {
            Error,
            Warning,
            Info,
            Success
        }

        private static string GetColorCode(InfoType InformationType)
        {
            switch (InformationType)
            {
                case InfoType.Error:
                    return "red";
                case InfoType.Info:
                    return "blue";
                case InfoType.Warning:
                    return "yellow";
                case InfoType.Success:
                    return "green";
                default:
                    return "blue";
            }
        }

        public static string ShowStatus(string Msg, InfoType InformationType)
        {
            string clr = GetColorCode(InformationType);
            StringBuilder sbAddStatus = new StringBuilder();
            sbAddStatus.AppendFormat(@"<script type=""text/ecmascript"" language=""ecmascript"">{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"    ExecuteOrDelayUntilScriptLoaded(AddStatus, ""sp.js"");{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"    function AddStatus() {{{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"        var sfsstatusId = SP.UI.Status.addStatus('" + InformationType.ToString() + " : ','" + SPHttpUtility.EcmaScriptStringLiteralEncode(Msg) + "');{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"        SP.UI.Status.setStatusPriColor(sfsstatusId, '" + clr + "');{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"    }}{0}", Environment.NewLine);
            sbAddStatus.AppendFormat(@"</script>");
            return sbAddStatus.ToString();
        }
    }
}
